package it.smlm.playbylocation;

import java.io.IOException;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.Log;

public class PlayerManager {

	private static final String TAG = "PlayerManager";
	
	private static PlayerManager playerManager = null;
	private MediaPlayer mediaPlayer = null;
	private AssetFileDescriptor dataSource = null;
	
	private boolean onPause;
	private boolean onStop;
	private boolean onIdle;
	private boolean onInitialized;
	
	public static PlayerManager getPlayerManager() {
		if(playerManager == null)
			playerManager = new PlayerManager();
		
		return playerManager;
	}
	
	private PlayerManager() {
		mediaPlayer = createMediaPlayer();
	}
	
	private MediaPlayer createMediaPlayer() {
		MediaPlayer mediaPlayer = new MediaPlayer();
		mediaPlayer.setOnCompletionListener(new OnCompletionL());
		
		onIdle = true;
		onInitialized = false;
		onStop = false;
		onPause = false;
		
		return mediaPlayer;
	}
    
    public MediaPlayer getMediaPlayer() {
    	if(mediaPlayer == null)
    		mediaPlayer = createMediaPlayer();
    	
    	return mediaPlayer;
    }
    
    public AssetFileDescriptor getDataSource() {
		return dataSource;
	}

	public void setDataSource(AssetFileDescriptor dataSource) {
		stopPlayer();
		this.dataSource = dataSource;
		loadPlayerDS();
	}

	public void startPlayer() {
    	
    	if(onIdle) {
    		loadPlayerDS();
    	}
    	
    	if(!onPause || onStop || onInitialized) {
    		try {
    			mediaPlayer.prepare();
    		} catch (IllegalStateException e) {
    			Log.e(TAG, "ERROR: " + e.getMessage(), e);
    			e.printStackTrace();
    		} catch (IOException e) {
    			Log.e(TAG, "ERROR: " + e.getMessage(), e);
    			e.printStackTrace();
    		}
    	}
    	
    	onIdle = false;
		onInitialized = false;
    	onStop = false;
    	onPause = false;
		
        mediaPlayer.start();
    }
    
	public void stopPlayer() {
    	if(mediaPlayer.isPlaying())
    		mediaPlayer.stop();

    	onIdle = false;
    	onInitialized = false;
    	onStop = true;
    	onPause = false;
    }
    
	public void pausePlayer() {
    	if(mediaPlayer.isPlaying()) {
    		mediaPlayer.pause();
    		
    		onIdle = false;
    		onInitialized = false;
    		onPause = true;
    		onStop = false;
    	}
    	
    }
    
	public void resetPlayer() {
    	mediaPlayer.reset();
    	
    	onIdle = true;
    	onInitialized = false;
    	onStop = false;
    	onPause = false;
    }
    
	public void releasePlayer() {
		mediaPlayer.release();
		
		onIdle = true;
		onInitialized = false;
    	onStop = false;
    	onPause = false;
	}
	
	public void loadPlayerDS() {
		
		try {
			mediaPlayer.setDataSource(dataSource.getFileDescriptor(), dataSource.getStartOffset(), dataSource.getLength());
			
			onIdle = false;
			onInitialized = true;
	    	onStop = false;
	    	onPause = false;
		} catch (IllegalArgumentException e) {
			Log.e(TAG, "ERROR: " + e.getMessage(), e);
			e.printStackTrace();
		} catch (IllegalStateException e) {
			Log.e(TAG, "ERROR: " + e.getMessage(), e);
			e.printStackTrace();
		} catch (IOException e) {
			Log.e(TAG, "ERROR: " + e.getMessage(), e);
			e.printStackTrace();
		}
    }
	
	public void setANewMediaPlayer() {
		mediaPlayer.stop();
		mediaPlayer.release();
		mediaPlayer = createMediaPlayer();
	}
	
	
	
	
	
	
	
	
	
	
	private class OnCompletionL implements OnCompletionListener {
		public void onCompletion(MediaPlayer mp) {
			mp.stop();
			mp.release();
		}
	}
}
